<?php
// +----------------------------------------------------------------------
// | Author: huangzhen_20210406
// +----------------------------------------------------------------------

namespace app\common\model;

use think\facade\Db;

class UserFee extends Common
{
    /**
     * 用过user访问user child
     * @param $post
     * @param $user_id
     * @return mixed
     * @throws \think\db\exception\DbException
     */
    public function tableDataAdmin($post)
    {
        $list_rows  = $post['limit'] ?? config('paginate.list_rows');
        $page       = $post['page'] ?? config('paginate.page');

        $where =[];
        if(isset($post['username']) && !empty($post['username'])){
            $where[] = ['u.username', 'like', '%' . trim($post['username']) . '%'];
        }
        if(isset($post['remark']) && !empty($post['remark'])){
            $where[] = ['uf.remark', 'like', '%' . trim($post['remark']) . '%'];
        }
        if(isset($post['user_type']) && strlen($post['user_type']) > 0){
            $where[] = ['u.user_type', 'like', $post['user_type']];
        }
        if(isset($post['datetime']) && !empty($post['datetime'])){
            $time_list = explode('_' , $post['datetime']);
            $start_time = strtotime($time_list[0] . " 00:00:00");
            $end_time   = strtotime($time_list[1] . " 23:59:59");
            $where[] = ['uf.ctime', 'between', "{$start_time},{$end_time}"];
        }
        $list = Db::name('user_fee')->alias('uf')
                ->leftJoin("user u", "u.id=uf.user_id")
                ->field('uf.* , u.username,u.user_type')
                ->where($where)
                ->order('uf.ctime desc')
                ->paginate(['list_rows' => $list_rows, 'page' => $page])
                ->toArray();
        $new_list = $list['data'];
        foreach ($new_list as &$v){
            if($v['type'] == 1){
                $v['amount'] = -$v['amount'];
            }
        }
        return $result = [
            'code'  => 0,
            'msg'   => lang('get info success'),
            'data'  => $new_list,
            'count' => $list['total'],
        ];
    }

    public function checkUserFee($user_id)
    {
        //获取本月的开始时间和结束时间
        $time       = getUtcMoneyTime();
        $start_time = $time['start_time'];
        $end_time   = $time['end_time'];
        //判断本月是否已收费过
        return Db::name('user_fee')
            ->where("user_id={$user_id} and (ctime between {$start_time} and {$end_time} )")
            ->find();
    }

    /**
     * @param $post
     * @param $user_id
     * @return mixed
     * @throws \think\db\exception\DbException
     */
    public function tableDataMonthDebt($post)
    {
        $list_rows  = $post['limit'] ?? config('paginate.list_rows');
        $page       = $post['page'] ?? config('paginate.page');

        $where =[];

        $where[] = ['debt', '>', '0'];

        if(isset($post['username']) && !empty($post['username'])){
            $where[] = ['username', 'like', '%' . trim($post['username']) . '%'];
        }
        if(isset($post['business_token']) && !empty($post['business_token'])){
            $where[] = ['business_token', 'like', '%' . trim($post['business_token']) . '%'];
        }
        if(isset($post['user_memo']) && !empty($post['user_memo'])){
            $where[] = ['user_num', 'like', '%' . trim($post['user_memo']) . '%'];
        }
        if(isset($post['user_type']) && strlen($post['user_type']) > 0){
            $where[] = ['user_type', 'like', $post['user_type']];
        }
        $list = Db::name('user')
            ->where($where)
            ->order('check_time desc')
            ->paginate(['list_rows' => $list_rows, 'page' => $page])
            ->toArray();

        return $result = [
            'code'  => 0,
            'msg'   => lang('get info success'),
            'data'  => $list['data'],
            'count' => $list['total'],
        ];
    }
}